package com.sto.stoapp;

import android.app.Application;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;

import com.sto.stoapp.broadcast.BatteryInfoReceive;
import com.sto.stoapp.broadcast.LogoutDelDataBroad;
import com.sto.stoapp.broadcast.ScreenReceiver;
import com.sto.stoapp.jpush.JPushHelper;
import com.sto.stoapp.manager.SoundManager;
import com.sto.stoapp.thread.DeleteApkThread;
import com.sto.stoapp.thread.UnInstallThread;
import com.sto.stoapp.widget.BaseActivity;
import com.umeng.analytics.MobclickAgent;

import java.io.File;

import sto.android.app.StoJNI;
import sto.android.app.StoTongJNI;

/**
 * Application
 */
public class StoApplication extends Application {

    public static final String TAG = "STO";
    private static StoApplication instance;
    private BatteryInfoReceive batteryReceive;
    public static final String DeviceModel_520 = "520";
    public static final String DeviceModel_521 = "521";
    public static final String DeviceModel_510 = "510";
    public static final String DeviceModel_420 = "420";
    public static final String DeviceModel_585 = "585";
    public String uniqueCode;
    LogoutDelDataBroad logoutReceiver;

    public static StoApplication getAppInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        //Add by wj 2016/11/03
        JPushHelper.initJpush(this);
        MobclickAgent.startWithConfigure(
                new MobclickAgent.UMAnalyticsConfig(this,"5851ec165312dd912e00042b","Sto",
                        MobclickAgent.EScenarioType.E_UM_NORMAL));
        //End by wj 2016/11/03

        instance = this;
        // 加载声音文件
        initSoundPool();
        //CrashHandler.getInstance().init(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        ScreenReceiver receiver = new ScreenReceiver();
        registerReceiver(receiver, filter);

        IntentFilter logoutFilter = new IntentFilter();
        logoutFilter.addAction(BaseActivity.CLOSE_THIS_ACTIVITY_ACTION);
        logoutReceiver = new LogoutDelDataBroad();
        registerReceiver(logoutReceiver, logoutFilter);
        batteryReceive = new BatteryInfoReceive();
        IntentFilter batteryFilter = new IntentFilter();
        batteryFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryReceive, batteryFilter);

//        if ((!StoConfig.USB_DEBUG)
//                && !Build.MODEL.trim().equals(DeviceModel_420))
//            StoApplication.getStoJni().TurnOffUSBDebug();

        deleteApk();

        new DeleteApkThread().start();

        //Add by wj
        new UnInstallThread(this).start();
        //End by wj


    }

    private void deleteApk() {
        File file = new File(Environment.getExternalStorageDirectory(),
                "STO.apk");
        if (file != null) {
            file.delete();
        }
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        if (logoutReceiver != null)
            unregisterReceiver(logoutReceiver);
        if (batteryReceive != null) {
            unregisterReceiver(batteryReceive);
        }
        System.exit(0);
    }

    /**
     * 初始化声音池
     */
    private void initSoundPool() {
        SoundManager soundManager = SoundManager.getInstance(this);
        soundManager.initSoundPool();
    }


    public static StoJNI getStoJni() {
        return StoTongJNI.getIns(StoApplication.getAppInstance());
    }

}
